<template>
  <div v-loading="loading">
    <a-alert message="此配置用于支付宝平台服务商分账" type="info" show-icon style="margin: 10px 0 20px 15px" />
    <a-form-model ref="wechatForm" :rules="rules" :model="params" :labelCol="{ span: 3 }" :wrapperCol="{ span: 12 }">
      <a-form-model-item label="应用AppId" prop="alipay_appid">
        <a-input v-model="params.alipay_appid" type="text" />
      </a-form-model-item>
      <a-form-model-item label="服务商 PID" prop="alipay_user_id">
        <a-input v-model="params.alipay_user_id" type="text" />
      </a-form-model-item>
      <a-form-model-item label="名称" prop="alipay_name">
        <a-input v-model="params.alipay_name" type="text" />
      </a-form-model-item>
      <a-form-model-item label="加密方式Aes_key" prop="alipay_aes_key">
        <a-input v-model="params.alipay_aes_key" type="text" />
      </a-form-model-item>
      <a-form-model-item label="支付宝公钥" prop="alipay_public_key">
        <a-textarea v-model="params.alipay_public_key" :autoSize="{ minRows: 4, maxRows: 6 }" />
      </a-form-model-item>
      <a-form-model-item label="应用公钥" prop="alipay_app_public_key">
        <a-textarea v-model="params.alipay_app_public_key" :autoSize="{ minRows: 4, maxRows: 6 }" />
      </a-form-model-item>
      <a-form-model-item label="应用私钥" prop="alipay_app_private_key">
        <a-textarea v-model="params.alipay_app_private_key" :autoSize="{ minRows: 4, maxRows: 6 }" />
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 24 }">
        <a-divider style="margin: 0 0 40px 0" />
        <div style="display: flex; justify-content: center">
          <a-button :loading="btnLoading" type="primary" @click="onSave" style="margin-right: 8px"> 保存 </a-button>
          <a-button @click="$router.back()"> 取消 </a-button>
        </div>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>
<script>
import { getAlipaySetting, setAlipaySetting } from '@/api/saas/setting'

export default {
  name: 'Alipay',
  components: {},
  data() {
    return {
      loading: false,
      btnLoading: false,
      params: {
        alipay_appid: '',
        alipay_public_key: '',
        alipay_app_public_key: '',
        alipay_app_private_key: '',
        alipay_user_id: '',
        alipay_name: '',
        alipay_aes_key: '',
      },
      rules: {
        alipay_appid: [{ required: true, message: '请输入应用AppId', trigger: 'blur' }],
        alipay_public_key: [{ required: true, message: '请输入支付宝公钥', trigger: 'blur' }],
        alipay_app_public_key: [{ required: true, message: '请输入应用公钥', trigger: 'blur' }],
        alipay_app_private_key: [{ required: true, message: '请输入应用私钥', trigger: 'blur' }],
        alipay_user_id: [{ required: true, message: '请输入服务商 PID', trigger: 'blur' }],
        alipay_name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        alipay_aes_key: [{ required: true, message: '请输入加密方式Aes_key', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getData()
  },
  methods: {
    onSave() {
      this.$refs.wechatForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          setAlipaySetting(this.params)
            .then((res) => {
              if (res.code == 0) {
                this.$message.success('保存成功！')
              } else {
                this.$message.warning(res.msg ? res.msg : '保存失败！')
              }
            })
            .catch((error) => {
              console.log(error)
            })
            .then(() => {
              this.btnLoading = false
            })
        }
      })
    },
    getData: function () {
      this.loading = true
      getAlipaySetting({})
        .then((res) => {
          if (res.code == 0) {
            this.params = res.data
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.loading = false
        })
    },
  },
}
</script>
<style lang="less" scoped>
</style>
